package com.software.myopiachildren;

import android.content.Intent;
import android.text.TextUtils;
import android.view.WindowManager;


import com.software.myopiachildren.base.BaseActivity;
import com.software.myopiachildren.content.Constant;
import com.software.myopiachildren.ui.login.GuideActivity;
import com.software.myopiachildren.ui.login.LoginActivity;
import com.software.myopiachildren.utils.sp.SP;

import java.util.Timer;
import java.util.TimerTask;


/**
 * 启动页
 *
 * @author penglei
 * @date 2019/05/12
 */
public class SplashActivity extends BaseActivity {
    private static final String TAG = "SplashActivity";
    Timer timer = new Timer();

    @Override
    protected int getContentViewId() {
        // 全屏、隐藏状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        return R.layout.activity_splash;
    }

    @Override
    public void initView() {
        initInter(); // 登录跳转判断
    }

    @Override
    public void initData() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    /**
     * 判断是否第一次启动
     */
    private void initInter() {
        // 延时
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                boolean first = new SP().load("first", true);
                if (first) {
                    new SP().save("first", false);
                    Intent intent = new Intent(SplashActivity.this, GuideActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    initLogin();
                }
            }
        }, 1000);
    }

    /**
     * 判断用户是否已登录
     */
    private void initLogin() {
        //自动登录 获取用户信息
        String userdata = new SP().load(Constant.SP_USER_DATA, "");
        // 判断用户信息是否存在，存在跳转到首页，不存在跳转到登录页
        if (!TextUtils.isEmpty(userdata)) {
            Intent intent = new Intent(SplashActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        } else {
            //在这里做判断--如果没有自动登录过，先判断是否进入过引导页
            Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
            startActivity(intent);
            finish();
        }
    }

}
